package com.ggb.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NioTest4 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("NioTest4input.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("NioTest4output.txt");
        FileChannel inputFileChannel = fileInputStream.getChannel();
        FileChannel outputFileChannel = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        while (true) {
            // 如果注释掉会一直读 原因：
            byteBuffer.clear();
            int read = inputFileChannel.read(byteBuffer);
            if (-1 == read) {
                break;
            }
            byteBuffer.flip();
            outputFileChannel.write(byteBuffer);
        }
        inputFileChannel.close();
        outputFileChannel.close();
    }
}
